﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
namespace ChinaClipping.ClippingMaster
{
    public partial class Dlg_ImportKeywordList : Form
    {
        private static Dlg_ImportKeywordList dlg = new Dlg_ImportKeywordList();
        private SoapFormatter _soapformatter = new SoapFormatter();
       private Dlg_ImportKeywordList()
        {
            InitializeComponent();
        }

       public static Dlg_ImportKeywordList GetDialog(TreeNode treeNode)
       {
           dlg.treeView_OriList.Nodes.Clear();
           dlg.treeView_OriList.Nodes.Add(treeNode);
           dlg.treeView_ImportList.Nodes.Clear();
           return dlg;
       }

       private void OK_Button_Click(object sender, EventArgs e)
       {
           this.DialogResult = DialogResult.OK;
           this.Hide();
       }

       private void Cancel_Button_Click(object sender, EventArgs e)
       {
           this.DialogResult = DialogResult.Cancel;
           this.Hide();
       }

       private void Dlg_ImportKeywordList_FormClosing(object sender, FormClosingEventArgs e)
       {
           if (e.CloseReason == CloseReason.UserClosing)
           {
               this.DialogResult = DialogResult.Cancel;
               e.Cancel = true;
               this.Hide();
           }
       }

       public TreeNode NewKeywordList
       {
           get 
           {
               return this.treeView_OriList.Nodes[0];
           }
       }

       private void button_SelectKeywordListFile_Click(object sender, EventArgs e)
       {
           if (openFileDialog_SelectKeywordListFile.ShowDialog() == DialogResult.OK)
           {
               if (loadKeywordList(openFileDialog_SelectKeywordListFile.FileName))
               {
                   this.button_Import.Enabled = false;
                   this.button_ImportAll.Enabled = true;
                   this.button_Overwrite.Enabled = true;
               }

           }
       }



       /// <summary>
       /// 加载关键词列表
       /// </summary>
       private bool loadKeywordList(string file)
       {
           bool success = false;
           if (File.Exists (file))
           {
               FileStream fs = null;
               try
               {
                   //fs = new FileStream(file, FileMode.Open);
                   //TreeNode tn = this._soapformatter.Deserialize(fs) as TreeNode;
                   TreeNode tn = TreeNodeHelper.GetTreeNodeFromFile(file);
                   tn.Text = "可导入的关键词";
                   setTreeNodeIcon(tn);
                   this.treeView_ImportList.BeginUpdate();
                   this.treeView_ImportList.Nodes.Clear();
                   this.treeView_ImportList.Nodes.Add(tn);
                   this.treeView_ImportList.EndUpdate();
                   success = true;

               }
               catch (Exception ex)
               {
                   MessageBox.Show("读取关键词列表时发生错误：" + ex.Message);
               }
               finally
               {
                   if (fs != null)
                       fs.Close();
               }
           }
           else
               MessageBox.Show("读取关键词列表时发生错误：文件不存在。");
           return success;
       }
       /// <summary>
       /// 重新设置TreeNode的ImageIndex及SelectedImageIndex属性，以解决反序列化后相关信息丢失的问题
       /// </summary>
       /// <param name="treeNode"></param>
       private void setTreeNodeIcon(TreeNode treeNode)
       {
           if (treeNode.Tag == null)
           {
               treeNode.ImageIndex = 1;
               treeNode.SelectedImageIndex = 3;
           }
           else
           {
               treeNode.ImageIndex = 0;
               treeNode.SelectedImageIndex = 0;
           }
           foreach (TreeNode tn in treeNode.Nodes)
           {
               setTreeNodeIcon(tn);
           }
       }

       private void button_Import_Click(object sender, EventArgs e)
       {
           TreeNode sourceNode = this.treeView_ImportList.SelectedNode;
           if (sourceNode == null)
           {
               MessageBox.Show("请选择要导入的内容。");
               return;
           }
           TreeNode desNode = this.treeView_OriList.SelectedNode;
           if (sourceNode == null)
               return;
           if (desNode == null)
               desNode = this.treeView_OriList.Nodes[0];
           else if (desNode.Tag != null && desNode !=this.treeView_OriList .Nodes [0])
               desNode = desNode.Parent;
           desNode.Nodes.Add(sourceNode.Clone() as TreeNode);
       }

       private void button_ImportAll_Click(object sender, EventArgs e)
       {
           if (this.treeView_ImportList.Nodes.Count == 0)
               return;
           TreeNode tn = this.treeView_ImportList.Nodes[0].Clone() as TreeNode;
           tn.Text = "导入的关键词";
           this.treeView_OriList.Nodes[0].Nodes.Add(tn);

       }

       private void button_Overwrite_Click(object sender, EventArgs e)
       {
           this.treeView_OriList.Nodes[0].Nodes.Clear();
           if(this.treeView_ImportList .Nodes.Count ==0)
               return;
           foreach (TreeNode tn in this.treeView_ImportList .Nodes [0].Nodes)
           {
               this.treeView_OriList.Nodes[0].Nodes.Add(tn.Clone() as TreeNode);
           }
       }

       private void treeView_ImportList_AfterSelect(object sender, TreeViewEventArgs e)
       {
           if (e.Node != treeView_ImportList.Nodes[0])
               button_Import.Enabled = true;
           else
               button_Import.Enabled = false;
       }
    }
}
